import assert from "assert";
import * as tf from "@tensorflow/tfjs-node";
import { denormalise, normalise } from "../utils/TensorUtils";
import { normalisedData } from "../types/normalisedData";

async function predict(
  normalisedFeature: normalisedData,
  normalisedLabel: normalisedData,
  model: tf.Sequential
  // outputData: Uint8Array | Int32Array | Float32Array
): Promise<number> {
  const predictedInput: number = testOutInt();

  let predictOut: number = 0;

  tf.tidy(() => {
    const inputTensor: tf.Tensor = tf.tensor1d([predictedInput]);
    const normalisedFeatureMin: tf.Tensor = normalisedFeature.min;
    const normalisedFeatureMax: tf.Tensor = normalisedFeature.max;
    const normalisedInput: normalisedData = normalise(
      inputTensor,
      normalisedFeatureMin,
      normalisedFeatureMax
    );

    //这里做预测
    const normalisedOutputTensor: tf.Tensor | tf.Tensor[] = model.predict(
      normalisedInput.tensor
    );

    const predictedTensor: tf.Tensor = denormalise(
      normalisedOutputTensor as tf.Tensor,
      normalisedLabel.min,
      normalisedLabel.max
    );

    // 转化为数字

    // 进行预测
    predictOut = predictedTensor.dataSync()[0];
  });
  return predictOut;
}

function testOutInt(): number {
  const predictedInput: number = parseInt("11451");
  assert(!isNaN(predictedInput));
  return predictedInput;
}

export { predict };
